﻿using System;
using System.IO;
using System.Linq;
using UnityEngine;

// Token: 0x02000531 RID: 1329
public class ExternalAudioClip
{
	// Token: 0x060021D3 RID: 8659 RVA: 0x000CDDD8 File Offset: 0x000CBFD8
	public static AudioClip Load(string path)
	{
		WaveHeader waveHeader = WaveHeader.ReadWaveHeader(path);
		if (waveHeader == null)
		{
			return null;
		}
		float[] array = ExternalAudioClip.CreateRangedRawData(path, waveHeader.TrueWavBufIndex, waveHeader.TrueSamples, (int)waveHeader.Channels, (int)waveHeader.BitPerSample);
		if (array.Length == 0)
		{
			return null;
		}
		string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
		return ExternalAudioClip.CreateClip(fileNameWithoutExtension, array, waveHeader.TrueSamples, (int)waveHeader.Channels, waveHeader.Frequency);
	}

	// Token: 0x060021D4 RID: 8660 RVA: 0x000CDE40 File Offset: 0x000CC040
	public static AudioClip CreateClip(string name, float[] rawData, int lengthSamples, int channels, int frequency)
	{
		AudioClip audioClip = AudioClip.Create(name, lengthSamples, channels, frequency, false);
		audioClip.SetData(rawData, 0);
		return audioClip;
	}

	// Token: 0x060021D5 RID: 8661 RVA: 0x000CDE64 File Offset: 0x000CC064
	public static float[] CreateRangedRawData(string path, int wavBufIndex, int samples, int channels, int bitPerSample)
	{
		byte[] array = File.ReadAllBytes(path);
		if (array.Length == 0)
		{
			return null;
		}
		return ExternalAudioClip.CreateRangedRawData(array, wavBufIndex, samples, channels, bitPerSample);
	}

	// Token: 0x060021D6 RID: 8662 RVA: 0x000CDE90 File Offset: 0x000CC090
	public static float[] CreateRangedRawData(byte[] data, int wavBufIndex, int samples, int channels, int bitPerSample)
	{
		float[] array = new float[samples * channels];
		int num = bitPerSample / 8;
		int num2 = wavBufIndex;
		try
		{
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = ExternalAudioClip.ByteToFloat(data, num2, bitPerSample);
				num2 += num;
			}
		}
		catch (Exception ex)
		{
			GlobalData.Debug_LogError(ex.ToString() + ": 不对应的声音文件: ");
			return Enumerable.Empty<float>().ToArray<float>();
		}
		return array;
	}

	// Token: 0x060021D7 RID: 8663 RVA: 0x000CDF14 File Offset: 0x000CC114
	private static float ByteToFloat(byte[] data, int index, int bitPerSample)
	{
		float result;
		if (bitPerSample != 8)
		{
			if (bitPerSample != 16)
			{
				throw new Exception(bitPerSample + "bit is not supported.");
			}
			short num = BitConverter.ToInt16(data, index);
			result = (float)num * ExternalAudioClip.RangeValue16Bit;
		}
		else
		{
			result = (float)(data[index] - 128) * ExternalAudioClip.RangeValue8Bit;
		}
		return result;
	}

	// Token: 0x04002327 RID: 8999
	public static readonly float RangeValue8Bit = 1f / Mathf.Pow(2f, 7f);

	// Token: 0x04002328 RID: 9000
	public static readonly float RangeValue16Bit = 1f / Mathf.Pow(2f, 15f);

	// Token: 0x04002329 RID: 9001
	public static readonly float RangeValue24Bit = 1f / Mathf.Pow(2f, 23f);

	// Token: 0x0400232A RID: 9002
	public static readonly float RangeValue32Bit = 1f / Mathf.Pow(2f, 31f);

	// Token: 0x0400232B RID: 9003
	public const int BaseConvertSamples = 20480;
}
